package src.com.unapec.seguridad.login.model;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

import javax.crypto.spec.IvParameterSpec;

public class Encryption {

	private String algorithm = "DES";
	private Cipher encrypter = null;
	private byte[] encryptionBytes = null;
	private SecretKey sKey = null;

	public Encryption() {
					
			try {
				Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
				SecretKey key = KeyGenerator.getInstance(algorithm).generateKey();

				// for CBC; must be 8 bytes
				byte[] initVector = new byte[] { 0x10, 0x10, 0x01, 0x04, 0x01, 0x01, 0x01, 0x02 };

				// * "algorithm/mode/padding" or
				// * "algorithm" 
				
				//Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");
				//"DES/CFB8/NoPadding"
				//"DES/OFB32/PKCS5Padding"
				//http://www.users.zetnet.co.uk/hopwood/crypto/scan/cs.html
				
				AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector);
				encrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");
				encrypter.init(Cipher.ENCRYPT_MODE, key, algParamSpec);
				
				        ByteArrayOutputStream bos = new ByteArrayOutputStream();
				        ByteArrayInputStream bis = new ByteArrayInputStream("Rudy Matos".getBytes());
				        CipherOutputStream cos = new CipherOutputStream(bos, encrypter);

				        int length = 0;
				        byte[] buffer =  new byte[8192];

				        while ((length = bis.read(buffer)) != -1) {
				           cos.write(buffer, 0, length);
				        }

				        bis.close();
				        cos.close();

				        System.out.println(bos.toString());
				    
				  
				
				FileInputStream fis = new FileInputStream("/home/rmatos/Desktop/cipherTest.in");
				FileOutputStream fos = new FileOutputStream("/home/rmatos/Desktop/cipherTest.out");
				int dataInputSize =  fis.available();
				
				byte[] inputBytes = new byte[dataInputSize];
				fis.read(inputBytes);
						
				
					//CipherOutputStream cos = new CipherOutputStream(fos, encrypter);
					cos.write(inputBytes, 0, inputBytes.length);
					cos.close();
				
					//write(inputBytes, fos);
				
				fos.flush();
				fis.close();
				fos.close();

//		    String inputFileAsString = new String(inputBytes);
//		    System.out.println("INPUT FILE CONTENTS\n" + inputFileAsString + "\n");
//
//		    System.out.println("File encrypted and saved to disk\n");
//
//		    fis = new FileInputStream("/home/rmatos/Desktop/cipherTest.out");
//
//		    byte[] decrypted = new byte[dataInputSize];
//		   
//		    CipherInputStream cis = new CipherInputStream(fis, m_decrypter);
//		    int pos = 0, intValue;
//
//		    while ((intValue = cis.read()) != -1) {
//		      decrypted[pos] = (byte) intValue;
//		      pos++;
//		    }
//		    
//		    fis.close();
//		    String decryptedAsString = new String(decrypted);
//
//		    System.out.println("DECRYPTED FILE:\n" + decryptedAsString + "\n");
			} catch (InvalidKeyException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchPaddingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvalidAlgorithmParameterException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		  }
		

	public static void main(String[] args) {

		Encryption e = new Encryption();
		//System.out.println(e.("Other Message"));
		//+BgI8U2nuDEzc1FhpSkuhg==

	}


}

